package compendiumAid.application.loadElement
{
	import compendiumAid.domain.model.ApplicationModel;
	import compendiumAid.domain.model.SessionModel;
	
	import org.spicefactory.lib.task.SequentialTaskGroup;
	import org.spicefactory.lib.task.TaskGroup;
	import compendiumAid.application.Element;

	public class LoadElementCommand
	{
		[Inject]
		public var model:ApplicationModel; 
		
		[Inject]
		public var sessionModel:SessionModel;
		
		[Command]
		public function loadElement(event:LoadElementEvent):void
		{
			if (sessionModel.parameters == null)
			{
				sessionModel.parameters = new Object;
				
				sessionModel.parameters.email = model.username;
				sessionModel.parameters.password = model.password;
				sessionModel.parameters.InsiderSignin = "Sign In";
				
				sessionModel.succesfulLogin = false;
			}
			
			load(event.element);
		}
		
		private function load(element:Element):void
		{
			var group:TaskGroup = new SequentialTaskGroup("LoadElement");
			group.ignoreChildErrors = true;
			group.addTask(new LoadElementTask(element, sessionModel));
			
			group.start();
		}
	}
}